package com.jf.game

import android.animation.ValueAnimator
import android.view.*
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.business.Analyse
import com.business.AnalyseKey
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.ads.NativeAdUtil
import com.helloandroid.ads.SmallNativeAdUtil
import com.helloandroid.ads.VideoAdUtil
import com.helloandroid.base.BaseDialogFragment
import com.helloandroid.tools.MyLog
import com.helloandroid.view.MySpannableString

class JFAwardDialog(private val jfAct:JfGame) : BaseDialogFragment(){

    lateinit var btnClose:ImageView
    lateinit var tvDrop:TextView
    lateinit var btnGet:Button

    private fun showTvDropAnim(){
        // 2s后显示出放弃按钮
        tvDrop.alpha = 0f
        tvDrop.postDelayed({
            tvDrop.alpha = 1f
            tvDrop.setOnClickListener { dismiss() }
        }, AppUtil.serverConfig.giveupDoubleRewardDelay.toLong())

//        val valueAnimator = ValueAnimator.ofFloat(0f,1f)
//        valueAnimator.duration = AppUtil.serverConfig.giveupDoubleRewardDelay.toLong()
//        valueAnimator.addUpdateListener {va->
//            tvDrop.alpha = va.animatedValue as Float
//            if (va.animatedFraction >= 1.0f){
//                MyLog.info("动画结束!")
//                tvDrop.setOnClickListener { dismiss() }
//            }
//        }
//        valueAnimator.start()
    }

    override fun initViews(view: View){
        // init views
        btnClose = findViewById(R.id.btnClose)
        tvDrop = findViewById(R.id.tvDrop)
        btnClose.visibility = View.INVISIBLE
        findBtn(R.id.btnClose).setOnClickListener { dismiss() }

        val  ivContent = findViewById<ImageView>(R.id.ivContent)
        val tvDesc = findViewById<TextView>(R.id.tvDesc)
        btnGet = findViewById(R.id.btnGet)

        // icon
        val jfResult = jfAct.mJFResult!!
        val imgRes = when(jfResult.result_id){
            JFViewModel.Fu -> R.drawable.jf_fu
            JFViewModel.Lu -> R.drawable.jf_lu
            JFViewModel.Shou -> R.drawable.jf_shou
            JFViewModel.Cai -> R.drawable.jf_cai
            JFViewModel.Xi -> R.drawable.jf_xi
            else -> when(jfResult.count){
                JFViewModel.SmallCoin -> R.drawable.jf_coin_1
                JFViewModel.MidCoin -> R.drawable.jf_coin_2
                else -> R.drawable.jf_coin_3
            }
        }
        ivContent.setBackgroundResource(imgRes)

        // desc
        if (jfResult.result_id == JFViewModel.Coin){
            //
            tvDesc.text = "恭喜获得金币${jfResult.count}"
            btnGet.visibility = View.VISIBLE
            btnGet.setOnClickListener { btnCoinClicked() }
            showTvDropAnim()
        }else{
            tvDesc.text = MySpannableString(requireContext(),"离100元奖励更近一步了哦")
                    .first("100").textColor(R.color.red)
            btnGet.visibility = View.INVISIBLE
            tvDrop.visibility = View.GONE
            btnClose.visibility = View.VISIBLE
        }

        // 广告
        SmallNativeAdUtil.showNativeAd(requireActivity(),findViewById(R.id.nativeView))
    }

    override fun initData() {
    }

    override fun getContentViewResId(): Int  = R.layout.dialog_jf_award

    override fun dialogTag(): String {
        return "jf_award_dialog"
    }

    private fun btnCoinClicked(){
        VideoAdUtil.playVideoAd(jfAct){success->
            if (success){
                Analyse.report(AnalyseKey.JF_VideoDoubleCoin)
                AppUtil.appViewModel().doubleAward(jfAct.mJFResult!!.count)
                tvDrop.visibility = View.GONE
                btnClose.visibility = View.VISIBLE
                btnGet.visibility = View.INVISIBLE
            }else{
                AppUtil.toast("视频还未准备好.")
            }
        }
    }

}